package com.leyou.auth.controller;

import com.leyou.auth.service.AuthService;
import com.leyou.common.auth.pojo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * User: ycs
 * Date: 2020/8/30
 * Time: 16:27
 *
 * @author: ycs
 * Description: No Description
 */
@RestController
public class AuthController {
    @Autowired
    private AuthService authService;
    @PostMapping("/login")
    public void logIn(@RequestParam("username") String username,
                      @RequestParam("password")String password,
                      HttpServletRequest request,
                      HttpServletResponse response){
        authService.logIn(username,password,request,response);
    }

    /**
     * 页面显示登录信息 登录验证
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/verify")
    public ResponseEntity<UserInfo> verify(HttpServletRequest request, HttpServletResponse response){
        UserInfo userInfo=authService.verify(request,response);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 退出登录
     * @param request
     * @param response
     * @return
     */
    @PostMapping("/logout")
    public ResponseEntity<Void>logout(HttpServletRequest request, HttpServletResponse response){
        authService.logout(request,response);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 给服务签发token
     * @param id
     * @param secret
     * @return
     */
    @GetMapping("/authorization")
    public ResponseEntity<String> authorization(@RequestParam("id") Long id ,@RequestParam("secret") String secret){
        return ResponseEntity.ok(authService.authorization(id,secret));
    }
}
